package greedy;

/**
 * Created with IntelliJ IDEA.
 * Description: 45. 跳跃游戏 II
 * User: Li_yizYa
 * Date: 2025/6/24
 * Time: 22:06
 */
public class Solution6 {
    public int jump(int[] nums) {
        if (nums == null || nums.length == 0 || nums.length == 1) {
            return 0;
        }
        int count = 0;// 记录跳的次数
        int curCoverRange = 0;// 当前覆盖区域
        int maxCoverRange = 0;// 最大覆盖区域
        for (int i = 0; i <= maxCoverRange; i++) {
            maxCoverRange = Math.max(maxCoverRange, nums[i] + i);
            if (maxCoverRange >= nums.length - 1) {
                count++;
                return count;
            }
            // 走到当前的最大覆盖区域时，更新下一步可到达的最大区域
            if (i == curCoverRange) {
                count++;
                curCoverRange = maxCoverRange;
            }
        }
        return count;
    }
}
